define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'commissionrules/index' + location.search,
                    add_url: 'commissionrules/add',
                    edit_url: 'commissionrules/edit',
                    del_url: 'commissionrules/del',
                    multi_url: 'commissionrules/multi',
                    import_url: 'commissionrules/import',
                    table: 'commissionrules',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text())==1) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: false,cellStyle: {css: {"max-width": "100px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}},
                        {field: 'goods_smoke', title: __('Goods_smoke')},
                        {field: 'favourable', title: __('Favourable')},
                        {field: 'region_divide', title: __('Region_divide')},
                        {field: 'merchant_smoke', title: __('Merchant_smoke')},
                        {field: 'kabaw_divide_profit', title: __('Kabaw_divide_profit')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row["id"] == 1) {
                                    $(table).data("operate-del", null);
                                    that.table = table;
                                }
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
